unit CheckDaemon;

interface

uses
  Classes, SysUtils, ProcessManager;

Type
  TCheckDaemon = Class(TComponent)
    private
      Flist: Tlist;
      FProcessManager: TProcessManager;
    public
      procedure Clear;
      procedure Add(const ProcessName:String);
      procedure LoadFromFile(aFileName:String);

      function Execute: Boolean;

      Constructor Create(AOnwer:TComponent); override;
      Destructor  Destroy; override;
    published
  End;


implementation

Type
  TItem = Class
    ProcessName,
    Description:String;
    Constructor Create(aProcessName, aDescription:String);
  End;

{ TCheckDaemon }

procedure TCheckDaemon.Add(const ProcessName: String);
var
  Item: TItem;
begin
  Item := TItem.Create(ProcessName, '');
  FList.Add( Item );
end;

procedure TCheckDaemon.Clear;
var
  I: Integer;
begin
  for I := FList.count - 1 downto 0 do
    TItem( FList.Items[i] ).Free;

  FList.Clear;
end;

constructor TCheckDaemon.Create(AOnwer: TComponent);
begin
  inherited;
  FList := TList.Create;
  FProcessManager := TProcessManager.Create(self);
end;

destructor TCheckDaemon.Destroy;
begin
  Self.Clear;
  FreeAndNil( FLIst );
  FreeAndNil( FProcessManager );
  inherited;
end;

function TCheckDaemon.Execute: Boolean;
var
  I: Integer;
  processName:String;
  Flag: Boolean;
begin
  if FLIst.Count = 0 then
  begin
    Result := True;
    Exit;
  end;

  Flag := True;

  FProcessManager.Open;
  for I := 0 to FList.Count - 1 do
  begin
    ProcessName := TItem( FList.Items[ i ] ).ProcessName;
    if not FProcessManager.ExistProcess(ProcessName) then
    begin
      Flag := False;
      break;
    end;
  end;

  Result := Flag;
end;

procedure TCheckDaemon.LoadFromFile(aFileName: String);
var
  mList:TStringLIst;
  I: Integer;
  Buff: string;
begin
  if not FileExists( aFileName ) then Exit;

  mList := TStringList.Create;
  try
    mLIst.LoadFromFile( aFileName );
    for I := 0 to mList.Count - 1 do
    begin
      Buff := mList.Strings[i];
      if Trim(Buff) = '' then Continue;
      self.Add( Buff );
    end;
  finally
    mList.Free;
  end;

end;

{ TItem }

constructor TItem.Create(aProcessName, aDescription: String);
begin
  
  ProcessName := aProcessName;
  Description := aDescription;
end;

end.
